package com.test.controller;

import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.Toast;
import android.widget.ToggleButton;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.test.R;

/**
 * 音量开关与滑动
 */

public class SwitchActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {

    private ToggleButton off_on;
    private Switch switch_status;

    @Override
    public void onCreate(@NonNull Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.switch_demo);//把该类放在容器中
        off_on = findViewById(R.id.off_on);
        switch_status = findViewById(R.id.switch_status);

        //this代表该类 SwitchActivity
        off_on.setOnCheckedChangeListener(this);
        switch_status.setOnCheckedChangeListener(this);
    }

    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
        switch (compoundButton.getId()) {
            case R.id.off_on:   //判断是哪一种方式的按钮（ToggleButton方式  点击）
                if (compoundButton.isChecked()) {// 判断打开的时候提示
                    Toast.makeText(this, "已打开声音", Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(this, "已关闭声音", Toast.LENGTH_LONG).show();
                }
                break;
            case R.id.switch_status:   //判断是哪一种方式的按钮 （switch方式按钮 滑动）
                if (compoundButton.isChecked()) {  // 判断打开的时候提示
                    Toast.makeText(this, "开关: ON", Toast.LENGTH_LONG).show();
                }
                Toast.makeText(this, "开关: OFF", Toast.LENGTH_LONG).show();
        }
    }
}
